﻿using System.ComponentModel;
using VNet.VisiSuite.Common.Resource;
using VNet.VisiSuite.VisiAdmin.Common.Helpers;
using VNet.VisiSuite.Controls;
using System.Windows;
using VNet.VisiSuite.VisiAdmin.Models;
using VNet.VisiSuite.VisiAdmin.Common;
using System.Collections.ObjectModel;

namespace VNet.VisiSuite.VisiAdmin.ViewModels
{
    public abstract class BaseViewModel : INotifyPropertyChanged
    {
        #region Implement INotifiedPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region Common Properties

        private string _pageTitle;
        public string PageTitle
        {
            get { return _pageTitle; }
            set
            {
                _pageTitle = value;
                OnPropertyChanged("PageTitle");
            }
        }

        private bool _isBusy;
        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                OnPropertyChanged("IsBusy");
            }
        }

        private string _errorMessage;
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set
            {
                _errorMessage = value;
                OnPropertyChanged("ErrorMessage");
            }
        }

        private ObservableCollection<VNetMenuItem> _topRightButtonIcons;
        public ObservableCollection<VNetMenuItem> TopRightButtonIcons
        {
            get { return _topRightButtonIcons; }
            set
            {
                _topRightButtonIcons = value;
                OnPropertyChanged("TopRightButtonIcons");
            }
        }

        private ObservableCollection<VNetMenuItem> _topLeftButtonIcons;
        public ObservableCollection<VNetMenuItem> TopLeftButtonIcons
        {
            get { return _topLeftButtonIcons; }
            set
            {
                _topLeftButtonIcons = value;
                OnPropertyChanged("TopLeftButtonIcons");
            }
        }

        private ObservableCollection<VNetMenuItem> _bottomMenuItems;
        public ObservableCollection<VNetMenuItem> BottomMenuItems
        {
            get { return _bottomMenuItems; }
            set
            {
                _bottomMenuItems = value;
                OnPropertyChanged("BottomMenuItems");
            }
        }

        public RelayCommand SaveCommand { get; set; }
        public RelayCommand AddCommand { get; set; }
        public RelayCommand CancelCommand { get; set; }
        public RelayCommand DeleteCommand { get; set; }

        public bool IsClosing { get; set; }

        public string Copyright
        {
            get { return string.Format("{0} {1}", TitleString.Footer, global::VNet.VisiSuite.VisiAdmin.App.ApplicationNameAndVersion()); }
        }

        public SaveChangesDialog ConfirmDialog
        {
            get { return new SaveChangesDialog("You have made changes that have not yet been saved to the Database.", "Save Change?"); }
        }

        public StaticData StaticData { get { return Constants.StaticData; } }

        #endregion

        #region Async method to invoke service

        private BackgroundWorker _worker;

        protected void DoAsync(DoWorkEventHandler doWork, RunWorkerCompletedEventHandler completed, object obj = null)
        {
            _worker = new BackgroundWorker { WorkerReportsProgress = false, WorkerSupportsCancellation = false };
            _worker.DoWork += doWork;
            _worker.RunWorkerCompleted += completed;
            _worker.RunWorkerAsync(obj);
        }

        #endregion

        #region Navigation framework

        public abstract void OnNavigate(object obj);

        public abstract void OnLeave();

        #endregion
    }
}